package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketCart;
import com.cskaoyan.bean.bo.CartAddBo;
import com.cskaoyan.bean.bo.CartCheckedBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.WxCartCheckedVo;
import com.cskaoyan.bean.vo.WxCartDataVo;
import com.cskaoyan.service.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * @author chengyi
 * @date 2022/10/14 15:17
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    WxCartService cartService;

    /**
     * 商品加购详情
     * @return
     */
    @RequestMapping("goodscount")
    public BaseRespVo goodCount(){
        int data = cartService.goodCount();
        return BaseRespVo.ok(data,"成功");
    }

    /**
     * 加入购物车
     * @param cartAddBo
     * @return
     */
    @RequestMapping("add")
    public BaseRespVo add(@RequestBody CartAddBo cartAddBo){
        Subject subject = SecurityUtils.getSubject();
        //判断用户是否登录
        int data=0;
        if(subject.isAuthenticated()){
            data = cartService.add(cartAddBo);
        }else{
            return new BaseRespVo(null,"请登录",501);
        }

        return BaseRespVo.ok(data,"成功");
    }

    /**
     * 立即购买
     * @param cartAddBo
     * @return
     */
    @RequestMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody CartAddBo cartAddBo){
        int data = cartService.fastAdd(cartAddBo);
        return BaseRespVo.ok(data,"成功");
    }

    @RequestMapping("selectAll")
    public BaseRespVo selectAll(){
        int data=cartService.selectAll();
        return BaseRespVo.ok();
    }

    /**
     * 显示整个购物车的信息
     * @return
     */
    @RequestMapping("index")
    public BaseRespVo index(){
        WxCartDataVo data = cartService.index();
        return BaseRespVo.ok(data,"成功");
    }

    /**
     * 修改购物车商品数量
     * @param marketCart
     * @return
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketCart marketCart){
        cartService.update(marketCart);
        return BaseRespVo.ok("成功");
    }

    /**
     * 勾选购物车
     * @param
     * @return
     */
    @RequestMapping("checked")
    public BaseRespVo checked( @RequestBody CartCheckedBo cartCheckedBo){
        WxCartDataVo data = cartService.checked(cartCheckedBo);
        return BaseRespVo.ok(data,"成功");
    }




    /**
     * 下单
     * @param cartId
     * @param addressId
     * @param couponId
     * @param userCouponId
     * @param grouponRulesId
     * @return
     */
    //cartId=0&addressId=71&couponId=0&userCouponId=0&grouponRulesId=0
    @RequestMapping("checkout")
    public BaseRespVo checkout(Integer cartId,Integer addressId,Integer couponId,Integer userCouponId,Integer grouponRulesId){
        WxCartCheckedVo data=null;
        //判断用户是否已登录
        if(SecurityUtils.getSubject().isAuthenticated()){
             data = cartService.checkout(cartId,addressId,couponId,userCouponId,grouponRulesId);
        }else{
             return new BaseRespVo(null,"请登录",501);
        }
        return BaseRespVo.ok(data,"成功");
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        WxCartDataVo data = cartService.delete(map);
        return BaseRespVo.ok(data,"成功");
    }

}
